package com.fengmi.cart.service;

import cn.hutool.json.JSONUtil;
import com.fengmi.api.goods.GoodsApi;
import com.fengmi.cart.Cart;
import com.fengmi.cart.filter.UserInfoInterceptor;
import com.fengmi.comm.ResultVO;
import com.fengmi.goods.MallGoodsSku;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.K;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Service
public class ICartServiceImpl implements ICartService {
    @Autowired
    private GoodsApi goodsApi;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ResultVO addCart(String skuId, Integer buyNum) {
        //skuId
        MallGoodsSku sku = goodsApi.getSkuById(Integer.valueOf(skuId));
        if(sku==null || sku.getSkuStock()<buyNum){
            return new ResultVO("1001","添加购物车失败",null);
        }
       //符合购买
        BoundHashOperations<String, String, String> cartRedis = redisTemplate.boundHashOps("fengmi:cart");
        String userId = UserInfoInterceptor.getUserInfo().getUserId();
        String s = cartRedis.get(userId);
        if(StringUtils.isEmpty(s)){ //购物车中没有此人的购物记录(new Cart())
            Cart cart = new Cart();
            cart.setSkuId(sku.getSkuId()+"");
            cart.setTitle(sku.getSkuTitle());
            cart.setImg(sku.getSkuImage());
            cart.setSpec(sku.getSkuSpec());
            cart.setPrice(sku.getSkuPrice().doubleValue());  //金额BigDecimal
            cart.setMarketPrice(sku.getSkuMarketPrice().doubleValue());
            cart.setBuyNum(buyNum);
            ArrayList<Cart> list = new ArrayList<>();
            list.add(cart);
            String cartlist = JSONUtil.toJsonStr(list);
            cartRedis.put(userId,cartlist);
        }else{   //redis中有此人的购买记录，在分两种情况
                     //1 购买过同种商品、购买数量相加
                     //2 从未买过此种商品
            List<Cart> carts = JSONUtil.toList(s, Cart.class);
            boolean flag=false; //默认没买过此种商品
           for( Cart c:carts){
               if(c.getSkuId().equals(skuId)){   //购买过
                   flag=true;
                   c.setBuyNum(c.getBuyNum()+buyNum);
               }
           }
           if(flag==false){   //从未买过此种商品
               Cart cart = new Cart();
               cart.setSkuId(sku.getSkuId()+"");
               cart.setTitle(sku.getSkuTitle());
               cart.setImg(sku.getSkuImage());
               cart.setSpec(sku.getSkuSpec());
               cart.setPrice(sku.getSkuPrice().doubleValue());  //金额BigDecimal
               cart.setMarketPrice(sku.getSkuMarketPrice().doubleValue());
               cart.setBuyNum(buyNum);
               carts.add(cart);
           }
            String s1 = JSONUtil.toJsonStr(carts);
            cartRedis.put(userId,s1);
        }
        return new ResultVO("1000","添加购物车成功",null);
    }

    @Override
    public ResultVO listCart() {
        BoundHashOperations<String, String, String> cartRedis = redisTemplate.boundHashOps("fengmi:cart");
        String userId = UserInfoInterceptor.getUserInfo().getUserId();
        String s = cartRedis.get(userId);
        List<Cart> carts = JSONUtil.toList(s, Cart.class);
        return new ResultVO("1000","查询成功",carts);
    }

    @Override
    public ResultVO updateCart(List<Cart> carts) {
        if(carts==null || carts.size()<=0){
            return  new ResultVO("1001","更新失败",null);
        }
        BoundHashOperations<String, String, String> cartRedis = redisTemplate.boundHashOps("fengmi:cart");
        String userId = UserInfoInterceptor.getUserInfo().getUserId();
        cartRedis.put(userId,JSONUtil.toJsonStr(carts));
        return new ResultVO("1000","更新成功",null);
    }

    @Override
    public ResultVO updateCartNew(List<Cart> carts) {
        //思路: 1 入参是购买的信息
             //2 根据入参，更新目前redis中的信息
        BoundHashOperations<String, String, String> cartRedis = redisTemplate.boundHashOps("fengmi:cart");
        String userId = UserInfoInterceptor.getUserInfo().getUserId();
        String s = cartRedis.get(userId);
        //购物车原来的信息
        List<Cart> carts1 = JSONUtil.toList(s, Cart.class);
        for(Cart cart:carts){  //传过来的购买的信息
            for(Cart cart1:carts1){//购物车现有的信息
                if(cart.getSkuId()==cart1.getSkuId()){
                    if(cart1.getBuyNum()==cart.getBuyNum()){
                        carts1.remove(cart1);
                    }else {
                        cart1.setBuyNum(cart1.getBuyNum() - cart.getBuyNum());   //做相减
                    }
                         //特殊情况， 差为0
                }
            }
        }
        cartRedis.put(userId,JSONUtil.toJsonStr(carts1));
        return new ResultVO("1000","更新成功",null);
    }
}
